package com.example.ssoreport.utils;

import java.io.Serializable;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 外部链接拼接ssotoken
 *
 * @author xlwang55
 * @date 2022/11/24 9:25
 */
public class HandleLinkUtil {

    /**
     * 公钥
     */
    public static final String LINK_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVZPYtiTX4vp51XYsezqMMl9Ne+zR7DrfuRRmAXSV8yiu9ldeQUIZS5n43UsLvzXxk/OZg5BvZblNo5SsWib54KMnaPacbJQDIBVswLAs0Y1Czr5X7KYtMHGKvNBqpEtRSZXyVGLaW7l5cqW80o466E47CNv7tZJVLmoo0PlYitQIDAQAB";
    /**
     * 格式化ssotoken
     */
    public static final String SSO_TOKEN_FORMATTING = "&ssotoken=%s";


    /**
     * @param param 获取外部链接入参
     * @return 返回拼接好的ssotoken链接
     * @author xlwang55
     * @date 2022/11/24
     */
    public static String getLink(ExternalLinkParamer param) throws Exception {
        Map<String, Serializable> map = new HashMap<>(4);
        //获取token
        map.put("username", param.getUserAccount());
        map.put("timestamp", System.currentTimeMillis());
        String ssoToken = URLEncoder.encode(EncryptUtils.rsaEncrypt(map.toString(), LINK_PUBLIC_KEY), "UTF-8");
        return param.getLink() + String.format(SSO_TOKEN_FORMATTING, ssoToken);
    }


}
